package com.neuedu.his.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neuedu.his.po.Constantitem;
import com.neuedu.his.po.User;
import com.neuedu.his.service.UserService;
import org.apache.catalina.Server;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {


    /**
     * 从容器中获取对象
     */
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public User login(@RequestParam String username,@RequestParam String password){
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("username",username).eq("password",password);
        List<User> users = userService.list(qw);
        if(users==null || users.size()==0)
            return null;
        else
            return users.get(0);
    }


    @RequestMapping("/list")
    public List<User> list(){

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        List list = userService.list(queryWrapper);

        return list;
    }




    /**
     *  添加后修改
     *  Post 请求
     * http://localhost:8080/user/saveOrUpdate
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    public boolean saveOrUpdate(@RequestBody  User user){
       boolean success = userService.saveOrUpdate(user);

        return success;
    }



    /**
     *  http://localhost:8080/user/getById/100
     *  http://localhost:8080/user/getById/100
     *  http://localhost:8080/user/getById/100
     */
    @RequestMapping("/getById/{id}")
    public User getById(@PathVariable  String id){
       User user  = userService.getById(id);
        return user;
    }



    /**
     *  http://localhost:8080/user/deleteById/100
     *  http://localhost:8080/user/deleteById/100
     *  http://localhost:8080/user/deleteById/100
     */
    @RequestMapping("/deleteById/{id}")
    public boolean deleteById(@PathVariable  String id){
        boolean success = userService.removeById(id);
        return success;
    }


    @RequestMapping("/getbydeptid")
    public List<User> getbydeptid(@RequestParam Integer deptid){
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("deptid",deptid).eq("usetype",3);
        return userService.list(qw);
    }

    @GetMapping("/settletypes")
    public List<User> getSettleTypescategory(
            @RequestParam(value = "category", required = false) Integer category,
            @RequestParam(value = "categoryid", required = false) Integer categoryId
    ) {
        Integer finalId = category != null ? category : categoryId;
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("categoryid", finalId)
                .eq("usetype", 3);
        return userService.list(qw);
    }
    @RequestMapping("/getdoctor")
    public List<User> getdoctor(@RequestParam Integer usetype){
        System.out.println("getdoctor   usetype="+usetype);
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("usetype",usetype);
        return userService.list(qw);
    }

    @RequestMapping("/getdoctorid")
    public Integer getdoctorid(@RequestParam String username) {
        System.out.println("getdoctorid   username=" + username);
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("username", username);

        // 获取用户列表
        List<User> users = userService.list(qw);

        // 检查是否找到用户
        if (users != null && !users.isEmpty()) {
            // 返回第一个匹配用户的ID
            return users.get(0).getId(); // 假设User类中有getUserId()方法
        } else {
            // 没有找到用户，返回null或适当的值
            return null;
        }
    }

    @RequestMapping("/getdeptid")
    public Integer getdeptid(@RequestParam String username) {
        System.out.println("getdeptid   username=" + username);
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.eq("username", username);

        // 获取用户列表
        List<User> users = userService.list(qw);

        // 检查是否找到用户
        if (users != null && !users.isEmpty()) {
            // 返回第一个匹配用户的ID
            return users.get(0).getDeptID(); // 假设User类中有getUserId()方法
        } else {
            // 没有找到用户，返回null或适当的值
            return null;
        }
    }

}
